//
//  YSAccountOrderStatusCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/7/31.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  订单提交状态

#import "YSAccountOrderStatusCtrl.h"
#import "YSOrderStatusFailure.h"
#import "YSOrderStatusSuccess.h"


@interface YSAccountOrderStatusCtrl ()
@property (strong, nonatomic)YSOrderStatusFailure *failureView;
@property (strong, nonatomic)YSOrderStatusSuccess *successView;
@property(nonatomic,copy)NSString *resultCode;
@property(nonatomic,assign)OrderStatusFinishType targetType;
@end

@implementation YSAccountOrderStatusCtrl

+(instancetype)initWithResultCode:(NSString *)resultCode targetType:(OrderStatusFinishType)targetType{
    YSAccountOrderStatusCtrl *result = [[YSAccountOrderStatusCtrl alloc]init];
    result.resultCode = resultCode;
    result.targetType = targetType;
    return result;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitle:@"结算中心"];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didNext:) name:kAccountOrderStatusNextNortif object:nil];
    
    self.failureView = [YSOrderStatusFailure failureView];
    self.successView = [YSOrderStatusSuccess successView];
    [self.failureView setWidth:SCREENW];
    [self.successView setWidth:SCREENW];
    [self.failureView setBackgroundColor:[UIColor clearColor]];
    [self.successView setBackgroundColor:[UIColor clearColor]];
    [self.failureView setY:100];
    [self.successView setY:100];
    
    [self checkPayStatus];
}

-(void)checkPayStatus{
    if(!ISBLANK(_resultCode)){
        
        switch (_resultCode.integerValue) {
            case 9000:// 支付成功
                NSLog(@"支付成功");
                [TalkingData trackEvent:kEventOrderCommitSuccess];
                [self.view addSubview:self.successView];
                if([DefaultsHelper boolForKey:kOrderType]){// 如果是体验券订单
                    self.successView.sendExpressLabel.hidden = YES;
                }
                break;
            case 4000:// 系统异常
                [TalkingData trackEvent:kEventOrderCommitFailure];
                [DefaultsHelper setObject:nil forKey:kOrderCode];//清空订单号
                NSLog(@"系统异常");
                [self.view addSubview:self.failureView];
                break;
            case 4001:// 订单参数错误
                NSLog(@"订单参数错误");
                [TalkingData trackEvent:kEventOrderCommitFailure];
                [DefaultsHelper setObject:nil forKey:kOrderCode];//清空订单号
               [self.view addSubview:self.failureView];
                break;
            case 6001:// 用户取消支付
                NSLog(@"用户取消支付");
                [TalkingData trackEvent:kEventOrderCommitFailure];
                [DefaultsHelper setObject:nil forKey:kOrderCode];//清空订单号
               [self.view addSubview:self.failureView];
                break;
            case 6002:// 网络连接异常
                NSLog(@"网络连接异常");
                [TalkingData trackEvent:kEventOrderCommitFailure];
                [DefaultsHelper setObject:nil forKey:kOrderCode];//清空订单号
                [self.view addSubview:self.failureView];
                break;
            default:
                break;
        }
    }
}

-(void)didNext:(NSNotification *)nortif{
    NSNumber  *type  =  [nortif object];
    if(type.integerValue == 1){//查看订单
        switch (_targetType) {
            case orderStatusFinishCartType:
                [_delegate orderStatusFinish:orderStatusFinishCartType];
                break;
            case orderStatusFinishOrderCenterType:
                [_delegate orderStatusFinish:orderStatusFinishOrderCenterType];
                break;
            case orderStatusFinishOrderDetailType:
                [_delegate orderStatusFinish:orderStatusFinishOrderDetailType];
                break;
            case orderStatusFinishProductDetailType:
                [_delegate orderStatusFinish:orderStatusFinishProductDetailType];
                break;
            default:
                break;
        }
    }else{//去逛逛
        [[NSNotificationCenter defaultCenter]postNotificationName:kToHomePageNortif object:@1];
    }
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
